/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


//import com.classnet.ClassNet;
import com.classnet.db.*;

import com.classnet.main.ClassNet;
import com.classnet.main.UserSession;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.Date.*;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Derek
 */
public class TempServlet extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servl et response
    */
    private String courseSubject; // the four letter thing, eg CPSC
    private String courseCode;  // eg 410
    private String section;     // homework? Exams?
    
    
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String messageReply = request.getParameter("messageReply");
        
        //long timeInMillis = System.currentTimeMillis();
        //Calendar c = Calendar.getInstance();
        //c.setTime(timeInMillis);
        //Date d = c.getTime();
        Date now = new Date(System.currentTimeMillis()); 
        //SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy HH:mm:ss a"); //MMMMM.
        SimpleDateFormat sdf = new SimpleDateFormat("kk:mm:ss, d MMM yyyy"); //MMMMM.
        String formattedDateString = sdf.format(now);


        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet PostServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet PostServlet at " + request.getContextPath () + "</h1>");
            out.println("<p> got reply message : </p>");
            out.println(messageReply);
            out.println("<p> we just need to get the \"replyer\"'s email address </p>");
            out.println("<p> and the time stamp like this : " +  formattedDateString.toString() + "</p>");
            out.println("</body>");
            out.println("</html>");
           
        } finally { 
            out.close();
        }
    } 
    
    protected void handleNewPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        courseSubject = request.getParameter("courseSubject");
        courseCode = request.getParameter("courseCode");
        //ClassNet c = ClassNet.INSTANCE;
        try{
        CoursePost cp = new CoursePost();
        cp.setTitle(request.getParameter("newPostTitle"));
        cp.setMessage(request.getParameter("newPostBody"));
        cp.setTitle(request.getParameter("newPostTitle"));
        cp.setPosted(new Date());
        
        HttpSession session = request.getSession(false);
        UserSession userSession = null;
        if(session == null) request.getRequestDispatcher("Login").forward(request, response);
        else userSession = (UserSession) session.getAttribute("usersession");
        
         
        if(userSession != null)
        {
            cp.setSender(userSession.getUser());
            userSession.persist(cp);
            
            
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Post result</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<p> Your message posted successfully.</p>");
            out.println("<p> You can close this window.</p>");
            out.println("<p> message ID : " + cp.getId()+" </p>");
            out.println("<p> courseSubject : " + request.getParameter("courseSubject")+" </p>");
            out.println("<p> courseCode : " + request.getParameter("courseCode")+" </p>");
            out.println("<p> section : " + request.getParameter("sectionSelection")+" </p>");
            out.println("<p> Title : " + request.getParameter("newPostTitle")+" </p>");
            out.println("<p> Body : " + request.getParameter("newPostBody")+" </p>");
            out.println("<p> timestamp : " + (new SimpleDateFormat("kk:mm:ss, d MMM yyyy")).format(new Date())+" </p>");
            //newPostTitle
            out.println("<button onclick=\"window.close()\">Close</button");
        //    out.println("<h1>User name " + username + " password : " + password +" </h1>");
            
            out.println("</body>");
            out.println("</html>");
            
        }
        else 
        {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Post result</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<p> You are not logged in.</p>");
            out.println("<p> Click <a href=\"LoginServlet\">here</a> to login</p>");
            out.println("<p> You can close this window.</p>");
            //out.println("<p> Title : " + request.getParameter("newPostTitle"))+" </p>");
            out.println("<p> section : " + request.getParameter("sectionSelection")+" </p>");
            out.println("<p> Title : " + request.getParameter("newPostTitle")+" </p>");
            out.println("<p> Body : " + request.getParameter("newPostBody")+" </p>");
            //newPostTitle
            out.println("<button onclick=\"window.close()\">Close</button");
        //    out.println("<h1>User name " + username + " password : " + password +" </h1>");
            
            out.println("</body>");
            out.println("</html>");
        }
        
        
        //User me = new User("chenjinlin@hotmail.com");
        
        //cp.setSender(me);    
        //c.getSession().persist(cp);
        
            
        } 
        finally { 
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        if(request.getParameter("newPost").compareToIgnoreCase("true") == 0)
        {
            handleNewPost(request,response);
        }    
        else
        {
            processRequest(request, response);
        }
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
